{% load thumbnail %}
{% load i18n %}
{% load staticfiles %}

{% with all_images=product.images.all %}
    {# use length rather then count as the images get queried anyways #}

    {% if all_images|length > 1 %}

        <div id="product_gallery" class="carousel slide">

            <div class="thumbnail">
                <div class="carousel-inner" role="listbox">
                {% for image in all_images %}
                    <div class="item {% if forloop.first %}active{% endif %}">
                        {% thumbnail image.original "440x400" upscale=False as thumb %}
                        <img src="{{ thumb.url }}" alt="{{ product.get_title }}" />
                        {% endthumbnail %}
                    </div>
                {% endfor %}
                </div>
                <a class="carousel-control left" href="#product_gallery" role="button" data-slide="prev">
                    <span class="icon-prev fa-stack fa-lg" aria-hidden="true">
                        <i class="fa fa-square-o fa-stack-2x"></i>
                        <i class="fa fa-angle-left fa-stack-1x"></i>
                    </span>
                </a>
                <a class="carousel-control right" href="#product_gallery" role="button" data-slide="next">
                    <span class="icon-next fa-stack fa-lg" aria-hidden="true">
                        <i class="fa fa-square-o fa-stack-2x"></i>
                        <i class="fa fa-angle-right fa-stack-1x"></i>
                    </span>
                </a>
            </div>

            <ol class="carousel-indicators thumbnail">
                {% for image in all_images %}
                    <li data-target="#product_gallery" data-slide-to="{{ forloop.counter0 }}" class="{% if forloop.first %}active{% endif %}">
                        {% thumbnail image.original "65x55" crop="center" as thumb %}
                        <img src="{{ thumb.url }}" alt="{{ product.get_title }}" />
                        {% endthumbnail %}
                    </li>
                {% endfor %}
            </ol>
        </div>

    {% else %}

        {# Only one image to show #}
        <div id="product_gallery" class="carousel">
            <div class="thumbnail">
                <div class="carousel-inner">
                    <div class="item active">
                    {% with image=product.primary_image %}
                        {% thumbnail image.original "440x400" upscale=False as thumb %}
                            <img src="{{ thumb.url }}" alt="{{ product.get_title }}" />
                        {% endthumbnail %}
                    {% endwith %}
                    </div>
                </div>
            </div>
        </div>

    {% endif %}
{% endwith %}
